//
//  PPAlbumContentTagsTableFooterView.m
//  pickpic
//
//  Created by Jinxiao on 9/16/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPAlbumContentTagsTableFooterView.h"

@interface PPAlbumContentTagsTableFooterView ()
@property (readwrite, nonatomic, assign) UIEdgeInsets insets;
@property (readwrite, nonatomic, assign) CGSize spaces;
@property (readwrite, nonatomic, strong) NSMutableArray <PPAlbumTagElement *> *tags;
@property (readwrite, nonatomic, strong) NSMutableArray <UIButton *> *buttons;
@end

@implementation PPAlbumContentTagsTableFooterView

- (instancetype)init {
    self = [super init];

    _tags = [[NSMutableArray alloc] init];
    _buttons = [[NSMutableArray alloc] init];

    _insets = UIEdgeInsetsMake(13, 15, 16, 13);

    _spaces = CGSizeMake(11, 10);

    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
}

- (void)reloadData {
    [_tags removeAllObjects];
    [_tags addObjectsFromArray:_hotTags];
    [_tags addObjectsFromArray:_customTags];

    [_buttons makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [_buttons removeAllObjects];

    for(NSInteger index = 0; index < _tags.count; index++) {

        PPAlbumTagElement *tag = _tags[index];

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.adjustsImageWhenHighlighted = NO;
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        button.contentEdgeInsets = UIEdgeInsetsMake(6, 6, 6, 6);
        button.layer.masksToBounds = YES;
        button.layer.cornerRadius = 3;
        [button setTitle:tag.name forState:UIControlStateNormal];
        [button setBackgroundImage:[[UIImage imageWithColor:[UIColor colorWithRGBHex:0xe0e0e0]] stretchableImageByCenter] forState:UIControlStateNormal];
        [button setBackgroundImage:[[UIImage imageWithColor:[UIColor colorWithRGBHex:0xb2b2b2]] stretchableImageByCenter] forState:UIControlStateHighlighted|UIControlStateSelected];
        [button setBackgroundImage:[[UIImage imageWithColor:[UIColor colorWithRGBHex:0xb2b2b2]] stretchableImageByCenter] forState:UIControlStateSelected];

        [button setTitleColor:[UIColor colorWithRGBHex:0x323232] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithRGBHex:0xffdd17] forState:UIControlStateHighlighted|UIControlStateSelected];
        [button setTitleColor:[UIColor colorWithRGBHex:0xffdd17] forState:UIControlStateSelected];

        [button sizeToFit];

        [self addSubview:button];

        [_buttons addObject:button];

        __weak typeof(self)weakSelf = self;
        [button setActionBlock:^(UIControl *control) {
            control.selected = !control.selected;

            if(control.selected) {
                [weakSelf.delegate footerView:weakSelf didSelectTag:tag];
            } else {
                [weakSelf.delegate footerView:weakSelf didDeselectTag:tag];
            }
        } forControlEvents:UIControlEventTouchUpInside];
    }

    CGFloat maximumWidth = self.width - _insets.left - _insets.right;
    CGFloat maximumHeight = 0;

    NSInteger numberOfLines = 0;

    BOOL initialized = NO;

    CGFloat pendingWidth = maximumWidth;
    CGFloat pendingOffsetX = 0;
    CGFloat pendingOffsetY = 0;
    CGFloat pendingMaximumHeightOfLine = 0;

    for(NSInteger index = 0; index < _buttons.count; index++) {
        UIButton *button = _buttons[index];

        CGSize buttonSize = button.bounds.size;

        if(pendingWidth < pendingOffsetX + buttonSize.width || !initialized) {
            pendingOffsetX = _insets.left;
            pendingOffsetY += pendingMaximumHeightOfLine + _spaces.height;
            pendingMaximumHeightOfLine = 0;
            numberOfLines++;
            initialized = YES;
        }

        button.frame = CGRectMake(pendingOffsetX, pendingOffsetY, buttonSize.width, buttonSize.height);

        pendingOffsetX += buttonSize.width + _spaces.width;
        pendingMaximumHeightOfLine = MAX(pendingMaximumHeightOfLine, buttonSize.height);

        [self addSubview:button];
    }

    maximumHeight = pendingMaximumHeightOfLine + pendingOffsetY + _insets.bottom;

    self.height = maximumHeight;

    [self setNeedsLayout];
}

- (void)selectTag:(PPAlbumTagElement *)tag {
    [_tags enumerateObjectsUsingBlock:^(PPAlbumTagElement *_Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
        if([obj.name isEqualToString:tag.name]) {
            UIButton *button = [_buttons safeObjectAtIndex:idx];
            button.selected = YES;
        }
    }];
}

- (void)deselectTag:(PPAlbumTagElement *)tag {
    [_tags enumerateObjectsUsingBlock:^(PPAlbumTagElement *_Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
        if([obj.name isEqualToString:tag.name]) {
            UIButton *button = [_buttons safeObjectAtIndex:idx];
            button.selected = NO;
        }
    }];
}

@end
